package platformer.controller.collisions;

import java.util.Collection;

import platformer.controller.collisions.CollisionDictionary;
import platformer.controller.collisions.effects.CollisionEffect;
import platformer.model.exceptions.ModelExceptions;

/**
 * The Class CollisionEngine.
 */

public class CollisionEngine {
	

	/** The dictionary. */
	private CollisionDictionary dictionary;

	/**
	 * Sets the dictionary.
	 *
	 * @param dictionary the new dictionary
	 */
	private void setDictionary(CollisionDictionary dictionary)
	{
		this.dictionary = dictionary;
	}

	/**
	 * Inits the private members.
	 *
	 * @param dictionary the dictionary
	 */
	private void init(CollisionDictionary dictionary) {
		this.setDictionary( dictionary );
	}
	
	/**
	 * Instantiates a new collision engine.
	 *
	 * @param dictionary the dictionary
	 */
	public CollisionEngine(CollisionDictionary dictionary) {
		init(dictionary);
	}
	
	/**
	 * Solve simple collision.
	 *
	 * @param collider the collider
	 * @param collided the collided
	 * @throws ModelExceptions the model exceptions
	 */
	private void solveSimpleCollision( Collisionable collider, Collisionable collided ) throws ModelExceptions
	{
	    Collection<CollisionEffect> effects = dictionary.getEffectsFor( collider, collided );
		
		for(CollisionEffect effect : effects)
			effect.applyFromTo( collided, collider );

	}

	/**
	 * Solve.
	 *
	 * @param collider the collider
	 * @param collided the collided
	 * @throws ModelExceptions the model exceptions
	 */
	public void solve( Collisionable collider, Collection<Collisionable> collided ) throws ModelExceptions
	{		
		for( Collisionable aCollided : collided ) {
			if( collider != aCollided ) { 	
				solveSimpleCollision( collider, aCollided );
				solveSimpleCollision( aCollided, collider );
			}
		}
	}

}
